/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.eplatform.modules.sys.web.officeinfo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.eplatform.common.config.Global;
import com.eplatform.common.persistence.Page;
import com.eplatform.common.web.BaseController;
import com.eplatform.common.utils.StringUtils;
import com.eplatform.modules.sys.entity.officeinfo.SysOfficeInfo;
import com.eplatform.modules.sys.service.officeinfo.SysOfficeInfoService;

/**
 * 协会简介Controller
 * @author H
 * @version 2016-06-20
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/officeinfo/sysOfficeInfo")
public class SysOfficeInfoController extends BaseController {

	@Autowired
	private SysOfficeInfoService sysOfficeInfoService;
	
	@ModelAttribute
	public SysOfficeInfo get(@RequestParam(required=false) String id) {
		SysOfficeInfo entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = sysOfficeInfoService.get(id);
		}
		if (entity == null){
			entity = new SysOfficeInfo();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:officeinfo:sysOfficeInfo:view")
	@RequestMapping(value = {"list", ""})
	public String list(SysOfficeInfo sysOfficeInfo, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<SysOfficeInfo> page = sysOfficeInfoService.findPage(new Page<SysOfficeInfo>(request, response), sysOfficeInfo); 
		model.addAttribute("page", page);
		return "modules/sys/officeinfo/sysOfficeInfoList";
	}

	@RequiresPermissions("sys:officeinfo:sysOfficeInfo:view")
	@RequestMapping(value = "form")
	public String form(SysOfficeInfo sysOfficeInfo, Model model) {
		sysOfficeInfo.setIntroduce(StringEscapeUtils.unescapeHtml4(sysOfficeInfo.getIntroduce()));
		model.addAttribute("sysOfficeInfo", sysOfficeInfo);
		return "modules/sys/officeinfo/sysOfficeInfoForm";
	}
	@RequiresPermissions("sys:officeinfo:sysOfficeInfo:view")
	@RequestMapping(value = "formbyoffice")
	public String formbyoffice(SysOfficeInfo sysOfficeInfo, Model model) {
		sysOfficeInfo=sysOfficeInfoService.getByOffice(sysOfficeInfo.getOffice().getId());
		if(sysOfficeInfo!=null){
		sysOfficeInfo.setIntroduce(StringEscapeUtils.unescapeHtml4(sysOfficeInfo.getIntroduce()));
		model.addAttribute("sysOfficeInfo", sysOfficeInfo);
		}
		return "modules/sys/officeinfo/sysOfficeInfoForm";
	}
	@RequiresPermissions("sys:officeinfo:sysOfficeInfo:edit")
	@RequestMapping(value = "save")
	public String save(SysOfficeInfo sysOfficeInfo, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, sysOfficeInfo)){
			return form(sysOfficeInfo, model);
		}
		boolean exists=sysOfficeInfoService.checkSingle(sysOfficeInfo);
		if(exists&&!StringUtils.isNotBlank(sysOfficeInfo.getId())){
			addMessage(redirectAttributes, "保存失败，协会简介不允许重复！");
		}
		else{
			sysOfficeInfoService.save(sysOfficeInfo);
			addMessage(redirectAttributes, "保存协会简介成功");
		}
		
		return "redirect:"+Global.getAdminPath()+"/sys/officeinfo/sysOfficeInfo/?repage";
	}
	
	@RequiresPermissions("sys:officeinfo:sysOfficeInfo:edit")
	@RequestMapping(value = "delete")
	public String delete(SysOfficeInfo sysOfficeInfo, RedirectAttributes redirectAttributes) {
		sysOfficeInfoService.delete(sysOfficeInfo);
		addMessage(redirectAttributes, "删除协会简介成功");
		return "redirect:"+Global.getAdminPath()+"/sys/officeinfo/sysOfficeInfo/?repage";
	}

}